<?php
namespace app\admin\controller;
use think\Db;
use changpay\ChangPay;
use app\common\controller\order\Orderget;
class Huankuan extends Common
{
//   还款记录列表页面
    public function index()
    {
		$qudao=$this->qudao();//调用渠道数据
		$this->assign('qudao',$qudao);
		$loanStatus = ['放款中', '已拒绝', '等待银行打款','待人工审核'];
		$this->assign('loanStatus',$loanStatus);
		return $this->fetch();	
    }
	//	 还款记录   用户 变化已完成的操作  加上展期操作的订单
    public function get_list()
    {
		$Type= input('Type');//还款类型
		$pay= input('pay');//还款方式
		$sorch= input('sorch');//2订单号 3姓名
		$sorch_aaa=input('sorch_aaa');//值
		$Orderget=new Orderget();
		$where=$Orderget->sorch($sorch,$sorch_aaa);
		if($Type){//还款类型
			$where['Type'] = $Type;
		}
		if($pay){//还款方式
			$where['a.pay'] = $pay;
		}
		if(empty($where['Type'])){
		 $where['Type']=array('in',[2,4,6,5,7]);//统计订单变成已完成的操作    <!--还款类型 1部分还款 2结清 3豁免 4线上还款 5线上展期 6线下还款 7线下展期--> 
			
		}
//		p($where);die;
		$page = input('page');
		$limit = input('limit')/2;
		$re=Db::table('xb_loans_hklist a')->
		field('d.daoqiMoney,a.moneysheng,b.TgadminID,a.TradeNo,b.Mobile,a.ApplyID,a.UserID,a.OrderSn,a.TradeNo,a.pay,a.TotalMoney,a.Type,d.ApplyMoney,d.AdoptMoney,d.YyFkTime,a.HkTime,b.TrueName')
		->page($page,$limit)->where($where)
		->order('a.ID','desc')
		->join('xb_mem_info b','a.UserID=b.id','LEFT')
		->join('xb_tg_admin c','b.TgadminID=c.ID',"LEFT")
		->join('xb_loans_applylist d','a.ApplyID=d.ID',"LEFT")
		->select();
        $re2=Db::table('xb_loans_xjapplylist a')->
		field('d.daoqiMoney,b.TgadminID,a.TradeNo,b.Mobile,a.AppID,a.UserID,a.OrderSn,a.TradeNo,a.pay,a.TotalMoney,a.Type,d.AdoptMoney,d.YyFkTime,a.PayTime,b.TrueName,d.ApplyMoney')
		->page($page,$limit)->where($where)
		->order('a.ID','desc')
		->join('xb_mem_info b','a.UserID=b.id','LEFT')
		->join('xb_tg_admin c','b.TgadminID=c.ID',"LEFT")
		->join('xb_loans_applylist d','a.AppID=d.ID',"LEFT")
		->select();
		foreach ($re2 as $k => $v) {
			$re2[$k]['HkTime']=$v['PayTime'];
		}
		$re=array_merge($re,$re2);
		foreach ($re as $k => $v) {
			if($v['Type']=='5' || $v['Type']=='7'){
//				echo 22;die;
				$re[$k]['YyFkTime']=$Orderget->get_time_jian($v['YyFkTime'],'6');//获取还款类型和支付方式
			}
		}
		
		
//		p($re);die;
		$re=$Orderget->get_Type_pay($re);//获取还款类型和支付方式
		foreach ($re as $k => $v) {
			$re[$k]['TgadminID']=Db::table('xb_tg_admin')->where(['ID'=>$v['TgadminID']])->value('Name');//获取渠道名称
			$re[$k]['jianmian_money']= Db::table('xb_loans_hklist')->where(['OrderSn'=>$v['OrderSn'],'Type'=>'3'])->sum('TotalMoney');//获取应还减免
		}
	 $count1=Db::table('xb_loans_hklist a')->
		field('a.moneysheng,b.TgadminID,a.TradeNo,b.Mobile,a.ApplyID,a.UserID,a.OrderSn,a.TradeNo,a.pay,a.TotalMoney,a.Type,d.ApplyMoney,d.AdoptMoney,d.YyFkTime,a.HkTime,b.TrueName')
		->where($where)
		->order('a.ID','desc')
		->join('xb_mem_info b','a.UserID=b.id','LEFT')
		->join('xb_tg_admin c','b.TgadminID=c.ID',"LEFT")
		->join('xb_loans_applylist d','a.ApplyID=d.ID',"LEFT")
		->count('a.ID');
        $count2=Db::table('xb_loans_xjapplylist a')->
		field('b.TgadminID,a.TradeNo,b.Mobile,a.AppID,a.UserID,a.OrderSn,a.TradeNo,a.pay,a.TotalMoney,a.Type,d.AdoptMoney,d.YyFkTime,a.PayTime,b.TrueName')
		->where($where)
		->order('a.ID','desc')
		->join('xb_mem_info b','a.UserID=b.id','LEFT')
		->join('xb_tg_admin c','b.TgadminID=c.ID',"LEFT")
		->join('xb_loans_applylist d','a.AppID=d.ID',"LEFT")
		->count('a.ID');
		$cuont=$count1+$count2;
		songsong($re,$cuont);
    }

}
